<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search" @isUnfold="
          (v) => {
            unfoldFlag = v;
          }
        ">
      </SearchForm>
    </template>
    <template v-slot:table>
      <TableData :tableThead="tableThead" :dataList="dataList" :loading="loading">
        <template v-slot:bseal="{ row }">
          <span>{{ row.bseal ? "是" : "否" }}</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import SearchForm from "@/components-production-management/SearchForm";
import TableData from "@/components-production-management/TableData";
//NC批次
import ncproductionApi from "@/api/production-management-nc/index";
import { getOneYearAgo, getCurrentDate } from "@/utils/time";

export default {
  components: { MainSlot, Pagination, SearchForm, TableData },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      page: 1,
      size:30,
      total: 10,
      searchFormData: {
        startDate: getOneYearAgo(),
        endDate: getCurrentDate(),
      },
      dataList: [],
      tableThead: [
        { isShow: true, label: "批次名称", prop: "vbatchcode" },

        {
          isShow: true,
          label: "物料名称",
          prop: "cmaterialvname",
          minWidth: "120px",
        },
      ],

      formItemList: [

        {
          label: "批次名称",
          type: "input",
          param: "vbatchcode",
          multiple: false,
          isShow: true,
        },
        {
          label: "物料名称",
          type: "input",
          param: "cmaterialvname",
          multiple: false,
          isShow: true,
        },
        {
          label: "日期查询",
          type: "daterange",
          param: "dateTime",
          multiple: false,
          defaultSelect: [getOneYearAgo(), getCurrentDate()],
          isShow: true,
        },
      ],
    };
  },
  mounted() {
    this.queryPageByCondition();
  },
  methods: {
    setOptions(param) {
      let options = [];
      if (param == "cdptid") {
        let list =
          JSON.parse(localStorage.getItem("NcOrgDept")) ||
          this.$store.state.NcBasicInfo.OrgDept;
        list.forEach((item) => {
          options.push({ text: item.name, value: item.pk_dept });
        });
      } else if (param == "pk_org") {
        let list =
          JSON.parse(localStorage.getItem("NcOrgOrgs")) ||
          this.$store.state.NcBasicInfo.OrgOrgs;
        list.forEach((item) => {
          options.push({ text: item.name, value: item.pk_org });
        });
      }
      return options;
    },
    queryPageByCondition() {
      let ncOrg = JSON.parse(localStorage.getItem("ncOrg")||"{}");
      let params = {
        page: this.page,
        size: this.size,
        entity: {
          ncOrg,
          ...this.searchFormData,
        },
      };
      this.loading = true;
      ncproductionApi
        .queryBatchcode(params)
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.message || "分页查询失败");
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      if (data.dateTime && data.dateTime.length) {
        data.startDate = data.dateTime[0];
        data.endDate = data.dateTime[1];
      }
      delete data.dateTime;
      this.searchFormData = { ...data };

      for (let key in this.searchFormData) {
        if (!this.searchFormData[key]) {
          this.searchFormData[key] = null;
        }
      }
      console.log(this.searchFormData, data, "this.searchFormData");
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
  },
};
</script>
<style lang="scss" scoped></style>
